package org.xyz.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.format.DateFormat;
import android.text.format.DateUtils;


public class DateTimeFormatter {
	
	private static final Calendar nowCalender = Calendar.getInstance();
	private static final Calendar sinceCalender = Calendar.getInstance();
	public static String formatTimeAsMMSS(String time){
		try{
			if(time.equals("0"))
				return "";
			long _t = Long.parseLong(time)/1000;
			int minute = (int) (_t/60);
			int second = (int) (_t%60);
			StringBuffer tmp = new StringBuffer();
			tmp.append(minute).append(":");
			if(second<9){
				tmp.append('0');
			}
			tmp.append(second);
			return tmp.toString();
		}catch(Exception e){
			
		}
		return "";
	}
	
	public static String format(Context context,String time) {
		if(StringHelper.isText(time)){
			long $time =-1;
			try{
				$time = Long.parseLong(time);
			}catch(NumberFormatException ne){
				return time;
			}
			return format(context,$time);
		}else {
			return "";
		}
	}
	public final static boolean isOneMinute(String s1,String s2){
		return isOneMinute(Long.parseLong(s1),Long.parseLong(s2));
	}
	public final static boolean isOneMinute(long t1,long t2)
	{
		boolean mbean = false;
		Date d1 = new Date(t1);
		Date d2 = new Date(t2);
		
		int m1 = d1.getMinutes();
		int m2 = d2.getMinutes();
		if(m1==m2){
			mbean = true;
		}
		return mbean;
	}

	public static String format(Context context, long targetTimeMillis) {
		if (DateUtils.isToday(targetTimeMillis)) {
			return DateFormat.getTimeFormat(context).format(
					new Date(targetTimeMillis));
		} else {
			// time since calendar
			sinceCalender.setTimeInMillis(targetTimeMillis);
			// nowadays calendar
			nowCalender.setTimeInMillis(System.currentTimeMillis());
			// time-since compare with nowadays
			if (sinceCalender.get(Calendar.YEAR) == nowCalender
					.get(Calendar.YEAR)
					&& nowCalender.get(Calendar.DAY_OF_YEAR)
							- sinceCalender.get(Calendar.DAY_OF_YEAR) == 1) {
				return "昨天";
			} else if (sinceCalender.get(Calendar.YEAR) == nowCalender
					.get(Calendar.YEAR)
					&& sinceCalender.get(Calendar.WEEK_OF_YEAR) == nowCalender
							.get(Calendar.WEEK_OF_YEAR)) {
				CharSequence weekday = DateFormat.format("EEEE",
						targetTimeMillis);
				return weekday.toString();
			} else {
				return DateFormat.getMediumDateFormat(context).format(
						new Date(targetTimeMillis));
			}
		}
	}
	
	@SuppressLint("SimpleDateFormat")
	public  static final SimpleDateFormat  df = new SimpleDateFormat("HH:mm:ss");
	/**
	 * 健康监测时间格式化
	 * @param time 系统当前时间
	 * @return
	 */
	public static final String formatAtHealthTime(long time){
		return df.format(new Date(time));
	}
	public static final String formatAsSqlTime(long time){
		return SQL_TIME_FORMAT.format(new Date(time));
	}
	public static final String formatAsSqlTime(Date date){
		return SQL_TIME_FORMAT.format(date);
	}
	public  static final SimpleDateFormat  SQL_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
}
